using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

/**
*@file       GWServer_RegisterReciever.cs
*@brief      process message GWServer_Register
*
*@author     graywind
*@version    1.0.0.1
*@data       2012/3/21
*/

//process GWServer_Register
public class OnGWServer_Register
{
    /// <summary>
    /// process message
    /// </summary>
    /// <param name="op">message operator</param>
    /// <param name="session_id">session identifier</param>
    /// <param name="msg_buffer">message buffer</param>
    /// <param name="msg_bytes">buffer length</param>
    public static void OnMapping(IMessageOperator op, Int32 session_id, byte[] msg_buffer, UInt32 msg_bytes)
    {
        //TODO
		gatewayserver.ServerInfo info = ProtoNet<gatewayserver.ServerInfo>.CreateFromBytes(msg_buffer, msg_bytes);

        if (op.ServerInfo.AddServerInfo(info) == true)
        {
            System.Console.WriteLine("Server Register With IP:{0}  Port:{1}  ServerType:{2}", info.IpAddr, info.Port, info.ServerType);
            if (info.ServerType == global.eServerType.ServerType_LoginServer)
            {
                if (info.IpAddr == op.AuthServerIP && info.Port == op.AuthServerPort)
                    op.AuthHandle = session_id;
            }

            if (info.ServerType == global.eServerType.ServerType_GameServer)
            {
                op.GameServerGroupInfo.SetGroupSessionId(info.WorldGroup, session_id);
            }

            if (info.ServerType == global.eServerType.ServerType_PosServer)
            {
                op.PosServerGroupInfo.SetGroupSessionId(info.WorldGroup, session_id);
            }

            msgGWServer_Register_Success registerSuc = new msgGWServer_Register_Success(session_id);
            gwMessagePort.ProcessMessage(registerSuc);
        }
    }
}

//register callback
partial class MessageProcTypes<ProcType, MessageProc>
{
        [MessageProcType((int)GatewayServerTypes.MessageType.GWServer_Register)]
        public static GatewayServerProcessor.GatewayServerTabFunc GWServer_RegisterProc = new GatewayServerProcessor.GatewayServerTabFunc(OnGWServer_Register.OnMapping);
}
